package org.example.cloud.payment.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.example.cloud.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 测试dashboard,查看结果
 *
 * 1.线程池维度是以"类"为维度分配的，加了@HystrixCommand所在的类会分配一个新线程池
 * 2.dashboard根据方法名称维度，会进行监控，加了@HystrixCommand的方法会有一个监控图（熔断器开和关都会显示）
 * 3.标注@HystrixCommand的方法都会在自己线程池中执行，不会使用tomcat线程
 */
@RestController
@RequestMapping("/payment3")
public class PaymentController3 {

    @Value("${server.port}")
    private Integer port;

    @Autowired
    private PaymentService paymentService;

    private void sleep(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            //e.printStackTrace();
        }
    }

    @HystrixCommand(fallbackMethod ="handle")
    @GetMapping("/hytrix-dashboard/ok")
    public String hytrixDashboard1(){
        System.out.println("执行开始--hytrixDashboard1");
        System.out.println(Thread.currentThread().getName()+"--hytrixDashboard1");
        System.out.println("执行结束--hytrixDashboard1");
        return "ok";
    }

    @HystrixCommand(fallbackMethod ="handle")
    @GetMapping("/hytrix-dashboard2/ok")
    public String hytrixDashboard2(){
        System.out.println("执行开始--hytrixDashboard2");
        System.out.println(Thread.currentThread().getName()+"--hytrixDashboard2");
        System.out.println("执行结束--hytrixDashboard2");
        return "ok";
    }

    @HystrixCommand(fallbackMethod ="handle")
    @GetMapping("/hytrix-dashboard3/ok")
    public String hytrixDashboard3(){
        System.out.println("执行开始--hytrixDashboard3");
        paymentService.print();
        System.out.println(Thread.currentThread().getName()+"--hytrixDashboard3");
        System.out.println("执行结束--hytrixDashboard3");
        return "ok";
    }

    private String handle(){
        System.out.println(Thread.currentThread().getName()+"--访问错误");
        return "error";
    }

}
